package com.primaryedu.ui.app.leave;

import com.primaryedu.R;
import com.primaryedu.bean.LeavelistBean;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

/**
 * 请假列表
 *
 * @author jun.wang
 * @description
 * @data 2018年1月4日
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public class LeaveListAdapter extends BaseAdapter {

    private Context context;
    LayoutInflater inflater;
    private List<LeavelistBean.DataBean.ListBean> list = new ArrayList<>();

    public LeaveListAdapter(Context context, List<LeavelistBean.DataBean.ListBean> list) {
        this.context = context;
        this.list = list;
        inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public void updateView(List<LeavelistBean.DataBean.ListBean> nowList) {
        this.list = nowList;
        this.notifyDataSetChanged();//强制动态刷新数据进而调用getView方法
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (null == convertView) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_leavelist, null);
            viewHolder.image = (ImageView) convertView.findViewById(R.id.image);
            //申请人，时间
            viewHolder.tv_name_time = (TextView) convertView.findViewById(R.id.tv_name_time);
            //请假类型
            viewHolder.leaveType = (TextView) convertView.findViewById(R.id.tv_leavetype);
            //原因
            viewHolder.reason = (TextView) convertView.findViewById(R.id.tv_reason);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        LeavelistBean.DataBean.ListBean lb = list.get(position);
        viewHolder.tv_name_time.setText("申请人：" + lb.getUserName() + "  " + lb.getCreateTime());
        if (lb.getType() == 0) {
            viewHolder.leaveType.setText("类型:事假");
        } else if (lb.getType() == 1) {
            viewHolder.leaveType.setText("类型:病假");
        }
        viewHolder.reason.setText(lb.getReason());

        if (lb.getStatus() == 0) {
            viewHolder.image.setBackground(context.getResources().getDrawable(R.drawable.leave_no_deal));
        } else if (lb.getStatus() == 1 || lb.getStatus() == 2) {
            viewHolder.image.setBackground(context.getResources().getDrawable(R.drawable.leave_deal));
        }

        return convertView;
    }

    public void addAll(List<LeavelistBean.DataBean.ListBean> leaveLists) {
        list.addAll(leaveLists);
        notifyDataSetChanged();
    }

    class ViewHolder {
        ImageView image;
        TextView reason;
        TextView leaveType;
        TextView tv_name_time;
    }

}
